<?php
class utilities {
	public static function getSpreadPath($id)
	{
		$path = '';
		$itemsNum = (float)$id;
		for($level = sfConfig::get('app_sf_folder_spread_levels_num'); $level > 0; $level--)
		{
			$itemsNum = ceil($itemsNum/sfConfig::get('app_sf_folder_items_max_num'));
			$folderName = ($level != 1 && $itemsNum > sfConfig::get('app_sf_folder_items_max_num')) ? sfConfig::get('app_sf_folder_items_max_num') : $itemsNum;
	
			$path = '/'.$folderName.$path;
		}
	 return substr($path, 1);
	}
	
	public static function getSpreadPathByDate($byDate='')
	{		
		$tmp1 = "";
		$tmp1 = $byDate;
		if(trim($byDate)=="") $tmp1 = date('Y-m-d h:i:s');
		$datetime = date_create($tmp1);
		return date_format($datetime, 'Y') . "/" . date_format($datetime, 'm') . "/" . date_format($datetime, 'd');
	}
	
	public static function deleteAll($directory, $empty = false) {
		if(substr($directory,-1) == "/") {
			$directory = substr($directory,0,-1);
		}
	
		if(!file_exists($directory) || !is_dir($directory)) {
			return false;
		} elseif(!is_readable($directory)) {
			return false;
		} else {
			$directoryHandle = opendir($directory);
		   
			while ($contents = readdir($directoryHandle)) {
				if($contents != '.' && $contents != '..') {
					$path = $directory . "/" . $contents;
				   
					if(is_dir($path)) {
						utilities::deleteAll($path);
					} else {
						unlink($path);
					}
				}
			}
		   
			closedir($directoryHandle);
	
			if($empty == false) {
				if(!rmdir($directory)) {
					return false;
				}
			}
		   
			return true;
		}
	}
	
	public static function slugify($str)
	{
		$tmp = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$tmp = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $tmp);
		$tmp = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $tmp);
		$tmp = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $tmp);
		$tmp = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $tmp);
		$tmp = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $tmp);
		$tmp = preg_replace("/(đ)/", 'd', $tmp);
		$tmp = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $tmp);
		$tmp = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $tmp);
		$tmp = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $tmp);
		$tmp = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $tmp);
		$tmp = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $tmp);
		$tmp = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $tmp);
		$tmp = preg_replace("/(Đ)/", 'D', $tmp);				
		$tmp = strtolower(trim($tmp));
		$tmp = str_replace(' ','_',$tmp);
		$tmp = str_replace('-','_',$tmp);
		$tmp = str_replace('.','',$tmp);
		$tmp = str_replace("'",'',$tmp);
		$tmp = str_replace('"','',$tmp);		
		$tmp = str_replace('“','',$tmp);
		$tmp = str_replace('”','',$tmp);
		$tmp = str_replace('‘','',$tmp);
		$tmp = str_replace('’','',$tmp);
		$tmp = str_replace('̀','',$tmp);		
		$tmp = str_replace('&','',$tmp);
		$tmp = str_replace('@','',$tmp);
		$tmp = str_replace('^','',$tmp);
		$tmp = str_replace('=','',$tmp);
		$tmp = str_replace('+','',$tmp);
		$tmp = str_replace(':','',$tmp);
		$tmp = str_replace(',','',$tmp);
		$tmp = str_replace('{','',$tmp);
		$tmp = str_replace('}','',$tmp);
		$tmp = str_replace('?','',$tmp);
		$tmp = str_replace('\\','',$tmp);
		$tmp = str_replace('/','',$tmp);
		$tmp = str_replace('quot;','',$tmp);
		return $tmp;
	} 
}
?>